/*
 * Copyright (c) 2018-2999 广州亚米信息科技有限公司 All rights reserved.
 *
 * https://www.gz-yami.com/
 *
 * 未经允许，不可做商业用途！
 *
 * 版权所有，侵权必究！
 */

package com.yami.shop.security.config;


import com.yami.shop.security.handler.LoginAuthFailedHandler;
import com.yami.shop.security.handler.LoginAuthSuccessHandler;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.factory.PasswordEncoderFactories;
import org.springframework.security.crypto.password.PasswordEncoder;


/**
 * @author LGH
 */
@Configuration
@Order(90)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    @Bean
    @SneakyThrows
    public AuthenticationManager authenticationManagerBean() {
        return super.authenticationManagerBean();
    }

    @Bean
    public PasswordEncoder passwordEncoder(){
        return PasswordEncoderFactories.createDelegatingPasswordEncoder();
    }


    //一下来自 https://gitee.com/leemon0512/spring-mall/?_from=gitee_search

    @Autowired
    private LoginAuthSuccessHandler loginAuthSuccessHandler;

    @Autowired
    private LoginAuthFailedHandler loginAuthFailedHandler;

//    @Override
//    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
//        auth.authenticationProvider(authenticationProvider);
//    }

//    @Override
//    protected void configure(HttpSecurity http) throws Exception {
//        http.csrf().disable()
//                .authorizeRequests()
//                .antMatchers("/**")
//                .permitAll();
//    }
//
//    @Bean
//    public LoginAuthenticationFilter loginAuthenticationFilter() {
//        LoginAuthenticationFilter filter = new LoginAuthenticationFilter();
//        try {
//            filter.setAuthenticationManager(authenticationManager());
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        filter.setAuthenticationSuccessHandler(loginAuthSuccessHandler);
//        filter.setAuthenticationFailureHandler(loginAuthFailedHandler);
//        return filter;
//    }

//    /**
//     * @author limenglong
//     * @create 2019-09-16 11:06
//     * @desc 登录认证过滤器
//     **/
//    public class LoginAuthenticationFilter extends UsernamePasswordAuthenticationFilter {
//
//        @Autowired
//        private AuthenticationTokenParser authenticationTokenParser;
//
//        @Override
//        public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response) throws AuthenticationException {
//            if (!request.getMethod().equals("POST")) {
//                throw new AuthenticationServiceException(
//                        "Authentication method not supported: " + request.getMethod());
//            }
//
//            //请求参数不限于username和password
//            String requestBody = null;
//            try {
//                requestBody = StreamUtils.copyToString(request.getInputStream(), Charset.defaultCharset());
//            } catch (IOException e) {
//                e.printStackTrace();
//            }
//
//            if (StringUtils.isBlank(requestBody)) {
//                throw new AuthenticationServiceException("无法获取输入信息");
//            }
//
//            //公共模块定制化，需要写通用接口
//            AbstractAuthenticationToken authRequest = authenticationTokenParser.parse(requestBody);
//
//            // Allow subclasses to set the "details" property
//            authRequest.setDetails(authenticationDetailsSource.buildDetails(request));
//
//            return this.getAuthenticationManager().authenticate(authRequest);
//
//        }



}
